<template>
  <div class="ele-body" v-loading="loading">
    <el-card shadow="none">
      <template #header>
        <div class="name">{{ info.customer ? info.customer.name : '' }}</div>
      </template>
      <template>
        <div class="top">
          <div class="bottom">
            <div v-for="item in info.histories" :key="item.id" class="list">{{ item.status }}</div>
          </div>
          <div class="closeWork">
<!--            <el-button type="primary" @click="toReceipt" :loading="loading">填写回单</el-button>-->
            <el-button v-if="isAssign&&!isClose" type="primary" @click="dispatchWork('')">指派工单</el-button>
            <el-button v-if="(!isClose||!isCompleteAudit)&&isAssigned" type="primary" @click="dispatchWork('transfer')">转派工单</el-button>
            <el-button v-if="isCompleteAudit&&permission.includes('sys:workTickets:audit')&&info.show_audit&&!isClose"
                       type="primary" @click="openAudit">审核
            </el-button>
            <el-button v-if="permission.includes('sys:workTickets:return')&&info.show_callback" type="primary"
                       @click="openReturn">回访
            </el-button>
            <el-button v-if="info.show_close&&permission.includes('sys:workTickets:close')" type="primary"
                       @click="closeWork" :loading="closeLoading">关闭工单
            </el-button>
          </div>
        </div>
      </template>
    </el-card>

    <!-- 工单信息 -->
    <div class="info-content">
      <el-card class="content-left" shadow="never">
        <div class="left-top">
          <div>工单编号：{{ info.wo_number }}</div>
          <!--<el-button size="mini" type="primary">编辑</el-button>-->
        </div>
        <div class="left-bottom-info">
          <div>工单类型： {{ info.type }}</div>
          <div>客户： {{ info.customer ? info.customer.name : '' }}</div>
          <div>联系人： <span >{{ info.contact_name }}</span></div>
          <div>电话： {{ info.contact_phone }}</div>
          <div>地址：
            <img alt="" src="../../../../assets/work_info/icon_map@2x.png" style="width: 13px;height: 16px;">
            <span style="color:var( --color-primary);margin-left: 5px;">
              {{ info.province_name }}{{ info.city_name }}{{ info.county_name }}{{ info.address }}
            </span>
          </div>
          <div>产品： <span >{{ productName }}</span></div>
          <div>计划时间： {{ info.plan_at }}</div>
          <div>负责人： <span >{{ userName }}</span></div>
          <div>协同人： <span >{{ collaborator }}</span></div>
          <div>工单状态：
            <el-button round size="mini" type="primary">{{ info.status }}</el-button>
          </div>
          <div>创建人： <span>{{ info.create_user ? info.create_user.nickname : '' }}</span></div>
          <div>创建时间： {{ info.create_time_format }}</div>
          <div>描述： {{ info.description }}</div>
          <!--          <div>派单人： {{((info.status_stream.map(item=>{return item.status==='已指派'&& item.active===1})).length!==0)?info.logs[1].creator:'' }}</div>-->
        </div>
      </el-card>
      <el-card class="right" shadow="never">
        <el-tabs v-model="tabsActive">
          <el-tab-pane label="动态信息" name="dtxx">
            <DynamicInfo ref="dynamicInfo" :logs="logs"/>
          </el-tab-pane>
          <el-tab-pane label="回执信息" name="hzxx" v-if="info.receipt">
            <ReceiptInfo ref="receipt" :receiptInfo="receiptInfo"/>
          </el-tab-pane>
          <el-tab-pane label="审核意见" name="shyj" v-if="info.audit_show?info.audit_show.length:false">
            <AuditInfo ref="audit" :auditInfo="auditInfo"/>
          </el-tab-pane>
          <el-tab-pane label="回访记录" name="hfjl" v-if="info.callback_show?info.callback_show.length:false">
            <ReturnInfo ref="return" :returnInfo="returnInfo"/>
          </el-tab-pane>
          <el-tab-pane label="签到信息" name="qdxx" v-if="info.checked">
            <SignIn_info ref="signIn" :id="id" @done="done"></SignIn_info>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <Audit ref="audit" :visible.sync="visible" :id="id" @done="done" @cancel="cancel"/>
    <Return ref="return" :returnVisible="returnVisible" :id="id" @done="done" @cancel="cancel"/>
<!--    <AddReceipt ref="addReceipt"/>-->
  </div>
</template>

<script>
import DynamicInfo from "./components/dynamic_info.vue"
import ReceiptInfo from "./components/receipt_info.vue"
import AuditInfo from "@/views/workTickets/workTickets/info/components/audit_info.vue";
import ReturnInfo from "@/views/workTickets/workTickets/info/components/return_info.vue";
import Audit from '@/views/workTickets/auditWork/compoents/index.vue'
import Return from '@/views/workTickets/returnWork/compoents/index.vue'
// import AddReceipt from './components/addReceipt.vue'
import SignIn_info from "@/views/workTickets/workTickets/info/components/signIn_info.vue";
// import { removePageTab } from '@/utils/page-tab-util'
import {mapGetters} from "vuex";


export default {
  components: {
    DynamicInfo,
    ReceiptInfo,
    Audit,
    Return,
    AuditInfo,
    ReturnInfo,
    // AddReceipt,
    SignIn_info
  },
  data() {
    return {
      closeLoading: false,
      //回访对话框
      returnVisible: false,
      //审核对话框
      visible: false,
      loading: false,
      id: null,
      tabsActive: "dtxx",
      info: {},
      logs: [],
      //审核信息
      auditInfo: {},
      //回访信息
      returnInfo: {},
      //回执信息
      receiptInfo: {},
    }
  },
  computed: {
    ...mapGetters(["permission"]),
    isCompleteAudit() {
      let flag = null
      if (this.info.status) {
        flag = this.info.status === '已完成'
      }
      return flag
    },
    isAssigned() {
      let flag = null
      if (this.info.status) {
        flag = this.info.status === '已指派'
      }
      return flag
    },
    isClose() {
      let flag = null
      if (this.info.status_stream) {
        flag = this.info.status === '已关闭'
      }
      return flag
    },
    isAssign() {
      let flag = null
      if (this.info.status) {
        flag = this.info.status === '已创建'
      }
      return flag
    },
    productName() {
      let name
      if (this.info.products) {
        name = this.info.products.map(item => {
          if (item.product) {
            return item.product.name
          }
        }).join(",")
      }
      return name;
    },
    userName() {
      let name;
      if (this.info.users) {
        this.info.users.map(item => {
          if (item.is_master === 1) {
            return name = item.user.realname
          }
        })
      }
      return name;
    },
    collaborator() {
      let name
      if (this.info.users) {
        name = this.info.users.map(item => {
          if (item.is_master === 0) {
            return item.user.realname
          }
        })
        return name.join(' ')
      }
      return name;
    },
  },
  mounted() {
    this.workInfo();
  },
  created() {
    this.id = this.$route.query.id;
  },
  methods: {
    //打开填写回执
    toReceipt() {
      this.$refs.addReceipt.showDialog(this.id);
    },
    openReturn() {
      this.returnVisible = true
    },
    openAudit() {
      this.visible = true
    },
    //取消
    cancel() {
      this.returnVisible = false
      this.visible = false
    },
    //完成
    done(id) {
      console.log(id, 22)
      if (id) {
        this.id = id
      }
      this.returnVisible = false
      this.visible = false
      this.workInfo();
      // removePageTab('/workTickets/workTickets/info')
      // this.$router.push({path:'/workTickets/workTickets'})
    },
    //指派工单
    dispatchWork(transfer) {
      this.$router.push({path: '/workTickets/pick-upsList', query: {id: this.id, transfer: transfer}})
    },
    //关闭工单
    closeWork() {
      this.$confirm('确定关闭工单?', '关闭工单', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //发起关闭工单请求
        this.closeLoading = true
        this.$http.post(`wos/${this.id}/close`).then(res => {
          if (res.data.code === 0) {
            this.closeLoading = false
            this.$message.success(res.data.msg);
            this.workInfo();
            // removePageTab('/workTickets/workTickets/info')
            // this.$router.push({path:'/workTickets/workTickets'})
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
      })
    },
    // 获取详情
    workInfo() {
      this.loading = true
      this.$http.get('wos/' + this.id).then(res => {
        if (res.data.code === 0) {
          this.info = res.data.data;
          this.logs = res.data.data.logs;
          this.receiptInfo = res.data.data.receipt
          this.auditInfo = res.data.data.audit_show
          this.returnInfo = res.data.data.callback_show
          //评价必须为单数据
          this.$nextTick(() => {
            if (this.info.receipt) {
              this.$refs.receipt.satisfaction = res.data.data.receipt.satisfaction
              this.$refs.receipt.attitude = res.data.data.receipt.attitude
              this.$refs.receipt.efficiency = res.data.data.receipt.efficiency
            }
          })
          this.$forceUpdate()
          this.loading = false
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.loading = false
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style lang="scss" scoped>


.work_info {
  width: 96%;
  margin: 50px auto 0;
}


.top {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  justify-content: space-between;
}

.bottom {
  display: flex;
  height: 100%;
  align-items:center;

  .list {
    width: 108px;
    height: 35px;
    text-align: center;
    line-height: 38px;
    box-shadow: 0 1px 1px rgba(0,0,0,.05), 0 2px 6px 0 rgba(0,0,0,.045);
    background-color: var(--color-primary-1);
    color: var(--color-primary);
    position: relative;
    margin: 0 15px 0 10px;
    letter-spacing: 5px;
    font-weight: bold;
    border-left: 4px solid var(--color-primary);
    border-radius: 4px 2px 2px 4px;
    &::after{
      content: " ";
      width: 0;
      height: 0;
      position: absolute;
      top:-1px;
      right: -7px;
      display: inline-block;
      border-color: transparent transparent transparent var(--color-primary-1);
      border-style: solid;
      border-width: 18px 0 18px 8px;
    }
  }
}


.info-content {
  height: calc(100vh - 268px);
  margin-top: 15px;
  display: flex;
  justify-content: space-between;

  .el-card + .el-card {
    margin-top: 0;
  }

  .content-left {
    width: 30%;
    box-sizing: border-box;
    overflow: auto;

    .left-top {
      font-size: 14px;
      color: #000000;
      font-weight: 600;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-bottom: 10px;
      border-bottom: 2px solid #eeeeee;
      box-sizing: border-box;
    }

    .left-bottom-info {
      font-size: 14px;

      div {
        padding: 10px 5px;
      }
    }
  }

  .right {
    //width: 68%;
    margin-left: 15px;
    flex: 1;
    overflow: auto;
  }
}
</style>
